How can I avoid having my Lambda function go into an inactive state?
The issue
I deployed my Lambda function as a container, but it becomes inactive after a few weeks of inactivity, causing invocation failures.
How can I avoid having my Lambda function go into an inactive state?
The solution
When a Lambda function is deployed as a container and not used for several weeks, it goes into an inactive state, as mentioned in the following AWS official documentation.
After you upload a new or updated container image, Lambda optimizes the image before the function can process invocations. The optimization process can take a few seconds. The function remains in the Pending state until the process completes. The function then transitions to the Active state.
If you invoke a Lambda function while it is in an inactive state, the invocation will fail.
Inactive – A function becomes inactive when it has been idle long enough for Lambda to reclaim the external resources that were configured for it. When you try to invoke a function that is inactive, the invocation fails and Lambda sets the function to pending state until the function resources are recreated. If Lambda fails to recreate the resources, the function returns to the inactive state.
To avoid your Lambda function from transitioning to an inactive state, there are two methods.
1. Set up provisioned concurrency
By setting up provisioned concurrency, you can avoid your Lambda function from going into an inactive state. However, please note that charges will incur even if the Lambda function is not invoked.
This is the number of pre-initialized execution environments allocated to your function. These execution environments are ready to respond immediately to incoming function requests. Provisioned concurrency is useful for reducing cold start latencies for functions. Configuring provisioned concurrency incurs additional charges to your AWS account.
2. Invoke the Lambda function periodically
By invoking the Lambda function at a frequency of about once a week, you can achieve this. Use services like Amazon EventBridge to schedule periodic invocations of the Lambda function.
References
- Create a Lambda function using a container image - AWS Lambda
- Lambda function states - AWS Lambda
- Configuring provisioned concurrency for a function - AWS Lambda
- Serverless Computing – AWS Lambda Pricing – Amazon Web Services
- Creating an Amazon EventBridge rule that runs on a schedule - Amazon EventBridge